import re
from datetime import datetime,timezone,timedelta

def to_timestamp(dt_str,tz_str):
    cday=datetime.strptime(dt_str,'%Y-%m-%d %H:%M:%S')
    tz = re.match(r"^UTC([+-]\d+):(\d+$)",tz_str).group(1)
    print(tz)
    print(cday)
    tz_utc=timezone(timedelta(hours=int(tz)))
    sure=cday.replace(tzinfo=tz_utc)
    print(sure)
    print(sure.timestamp())
    return sure.timestamp()
def to_timestamp2(dt_str, tz_str):
    dt_time = datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S')
    m = re.match('UTC([+-])(\d+):(\d+)',tz_str)
    sign = m.group(1) # 获取正负号
    hour = int(sign+m.group(2)) # 如果时区为正，设置hour为正值；时区为负，则设置为负值
    minute = int(sign+m.group(3)) # minute与hour同理
    tz_utc_8 = timezone(timedelta(hours=hour,minutes=minute))
    dt_time = dt_time.replace(tzinfo=tz_utc_8) #强制设置时区，并将返回的新的datatime对象赋给dt_time
    return dt_time.timestamp()
# 测试:
t1 = to_timestamp('2015-6-1 08:10:30', 'UTC+7:00')
assert t1 == 1433121030.0, t1

t2 = to_timestamp('2015-5-31 16:10:30', 'UTC-09:00')
assert t2 == 1433121030.0, t2